Column

Visualisation 1

Column

Visualisation 2

Visualisation 3

---
title: "Un joli titre pour le dashboard"
author: "Sofia Chemolli & Amélie Rivet"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    social: menu
    source_code: embed
---

   


```{r setup, include=FALSE}
library(ggplot2)
library(tidyverse)
library(dplyr)
library(plotly)
library(flexdashboard)
```

Column {data-width=500}
-----------------------------------------------------------------------

### Visualisation 1

```{r data, eval = TRUE, echo = FALSE, results='hide', message = FALSE, warning=FALSE}
# Espérance de vie a la naissance
dta_off <- read.csv("Dta_off.csv", sep=",", stringsAsFactors = T)
dta_off <- dta_off[,3:23]
colnames(dta_off) <- c("Country","ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")

dta_off <- dta_off %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(dta_off)[3:4] <- c("Year", "Life.expectancy")
dta_off$Year <- as.factor(dta_off$Year)

dta_off <- dta_off[-c(4558:nrow(dta_off)),]


# GDP par habitant
dta_GDP <- read.csv("GDPDataBank.csv", sep=",", stringsAsFactors = T)
dta_GDP <- dta_GDP[,4:23]
colnames(dta_GDP) <- c("ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")

dta_GDP <- dta_GDP %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(dta_GDP)[2:3] <- c("Year", "GDP")
dta_GDP$Year <- as.factor(dta_GDP$Year)


# Dépenses sante % GDP
dta_dep <- read.csv("HealthExpDataBank.csv", sep=",", stringsAsFactors = T)
dta_dep <- dta_dep[,4:23]
colnames(dta_dep) <- c("ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")

dta_dep <- dta_dep %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018"))
colnames(dta_dep)[2:3] <- c("Year", "Depense")
dta_dep$Year <- as.factor(dta_dep$Year)


# Anemie chez les enfants
dta_anemia <- read.csv("WHO_anemia.csv", sep=';', dec=',', stringsAsFactors = T)
dta_anemia$Year <- as.factor(dta_anemia$Year)


# Acces a l'eau potable
dta_water <- read.csv("WaterDataBank.csv", sep=",", stringsAsFactors = T)
dta_water <- dta_water[,4:23]
colnames(dta_water) <- c("ISO3","2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015", "2016","2017","2018")

dta_water <- dta_water %>% 
  pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015", "2016","2017","2018"))
colnames(dta_water)[2:3] <- c("Year", "Water")
dta_water$Year <- as.factor(dta_water$Year)

dta <- left_join(dta_off, dta_dep, by = c("ISO3","Year"))

dta <- left_join(dta, dta_GDP, by = c("ISO3","Year"))

dta <- left_join(dta, dta_anemia[,-2], by = c("ISO3","Year"))

dta <- left_join(dta, dta_water, by = c("ISO3","Year"))

# Amélie
dta.diff <- dta %>% 
  group_by(ISO3) %>%
  summarise(Life.expectancy.diff = Life.expectancy[Year=='2018'] - Life.expectancy[Year=='2000'], 
            Depense.diff = Depense[Year=='2018'] - Depense[Year=='2000'], 
            GDP.diff = GDP[Year=='2018'] - GDP[Year=='2000'],
            Anemie.diff = Anemie[Year=='2018'] - Anemie[Year=='2000'], 
            Water.diff = Water[Year=='2018'] - Water[Year=='2000']) %>%
  arrange(Life.expectancy.diff)

# Sofia
diff.ISO3 <- dta_off %>% 
  filter(Year==2000 | Year==2018) %>% 
  group_by(ISO3) %>%
  summarise(Life.expectancy.diff = Life.expectancy[Year=='2018'] - Life.expectancy[Year=='2000'],
            Country = Country,
            Life.expectancy.2000 = Life.expectancy[Year=='2000'],
            Life.expectancy.2018 = Life.expectancy[Year=='2018']) %>% 
  arrange(Life.expectancy.diff) 
```

```{r vis1,  echo = FALSE,  message = FALSE, warning=FALSE}
fig <- plot_ly(diff.ISO3, 
               
               type = 'choropleth', 
               
               locations = diff.ISO3$ISO3, 
               
               z = diff.ISO3$Life.expectancy.diff, 
               
               text = paste("Pays : ", diff.ISO3$Country, '
', "Esperance de vie en 2000 : ", round(diff.ISO3$Life.expectancy.2000,1), '
', "Esperance de vie en 2018 : ", round(diff.ISO3$Life.expectancy.2018,1), '
', "Difference : ", round(diff.ISO3$Life.expectancy.diff,1)), colorscale = "YlGnBu", hoverinfo = 'text') m <- list(l = 50, r = 50, b = 100, t = 100, pad = 4) fig %>% colorbar(title = list( text = paste0("Différence de \nl'espérance de vie \nà la naissance \nentre 2018 et 2000 \n(en années)"), font = list(size=12)), x = 1, y = 0.7, len = 0.4) %>% layout(title = list(text = paste0("Différence de l'espérance de vie moyenne par pays" , '
', '', "entre 2018 et 2000"), font=list(size=20), xanchor = "middle", y=0.8, autosize = F, width = 500, height = 500, margin = m)) ``` Column {.tabset data-width=500} ----------------------------------------------------------------------- ### Visualisation 2 ```{r, eval = TRUE, echo = FALSE, results='hide', message = FALSE, warning=FALSE, fig.keep='none'} expectancy <- inner_join(dta, dta.diff, by='ISO3') expectancy.2000 <- expectancy %>% filter(Year==2000) expectancy.2000[,4:13]<-round(expectancy.2000[,4:13],1) summary(expectancy.2000) ``` ```{r, echo = FALSE, message = FALSE, warning=FALSE} g1 <- ggplotly(ggplot(data = expectancy.2000, aes(x = GDP.diff, y = Life.expectancy.diff, text=paste("Pays : ",Country, "
Espérance de vie en 2000 : ", Life.expectancy, "
GDP en 2000 : ", GDP)), na.rm=TRUE) + geom_point(aes(color=Life.expectancy, size=GDP)) + scale_color_gradient(low ="#FFFF99",high = "#009933") + scale_y_continuous(breaks=seq(-10, 20, 5)) + scale_x_continuous(breaks=seq(-1000, 101000, 25000)) + labs(x = "Différence de GDP par habitant \nentre 2000 et 2018", y = "Différence d'espérance de vie \nentre 2000 et 2018", colour = "Espérance de vie \n en 2000", size = "GDP par habitant \n en 2000") + ggtitle("Représentation de l'espérance de vie en fonction du GDP par habitant") + theme(plot.title = element_text(face="bold", hjust=0.5, size=10), axis.title.x = element_text(size=9), axis.title.y = element_text(size=9), panel.background = element_rect(fill = "white"), panel.grid.major = element_line(colour = "#CCCCCC"), plot.margin = unit(c(1,1,1,1), "cm")), tooltip="text") # g1 %>% layout(showlegend=TRUE) ``` ```{r, echo = FALSE, message = FALSE, warning=FALSE} g2 <- ggplotly(ggplot(data = expectancy.2000, aes(x = Depense.diff, y = Life.expectancy.diff, text = paste("Pays : ", Country, "
Espérance de vie en 2000 : ", Life.expectancy, "
Dépenses en santé en % GDP en 2000 : ", Depense," %")), na.rm=TRUE) + geom_point(aes(color=Life.expectancy, size=Depense)) + scale_color_gradient(low ="#FFFF99",high = "#009933") + scale_y_continuous(breaks=seq(-10, 20, 5)) + scale_x_continuous(limits=c(-5,7.5), breaks=seq(-5, 7.5, 2.5)) + labs(x = "Différence du pourcentage de dépenses \nen santé en pourcentage du GDP \nentre 2000 et 2018", y = "Différence d'espérance de vie \nentre 2000 et 2018", colour = "Espérance de vie \n en 2000", size = "Dépenses en santé \n par habitant en 2000") + ggtitle("Représentation de la relation espérance de vie - dépense en santé") + theme(plot.title = element_text(face="bold", hjust=0.5, size=10), axis.title.x = element_text(size=9), axis.title.y = element_text(size=9), panel.background = element_rect(fill = "white"), panel.grid.major = element_line(colour = "#CCCCCC"), plot.margin = unit(c(1,1,1,1), "cm")), tooltip="text") # g2 %>% layout(showlegend=TRUE) ``` ```{r, echo = FALSE, message = FALSE, warning=FALSE} g3 <- ggplotly(ggplot(data = expectancy.2000, aes(x = Anemie.diff, y = Life.expectancy.diff, text=paste("Pays : ",Country, "
Espérance de vie en 2000 : ", Life.expectancy, "
Enfants de moins de 5 ans \n atteint d'anémie en 2000 : ", Anemie, "%")), na.rm=TRUE) + geom_point(aes(color=Life.expectancy, size=Anemie)) + scale_color_gradient(low ="#FFFF99",high = "#009933") + scale_y_continuous(breaks=seq(-10, 20, 5)) + scale_x_continuous(limits=c(-30, 5), breaks=seq(-30,5,5))+ labs(x = "Différence de pourcentage d'enfants de moins de \n5 ans atteints d'anémie \nentre 2000 et 2018", y = "Différence d'espérance de vie \nentre 2000 et 2018", colour = "Espérance de vie \n en 2000", size = "% d'enfant atteints \n d'anémie en 2000") + ggtitle("Représentation de la relation espérance de vie - Anémie") + theme(plot.title = element_text(face="bold", hjust=0.5, size=10), axis.title.x = element_text(size=9), axis.title.y = element_text(size=9), panel.background = element_rect(fill = "white"), panel.grid.major = element_line(colour = "#CCCCCC"), plot.margin = unit(c(1,1,1,1), "cm")), tooltip="text") # g3 %>% layout(showlegend=TRUE) ``` ```{r, echo = FALSE, message = FALSE, warning=FALSE} g4 <- ggplotly(ggplot(data = expectancy.2000, aes(x = Water.diff, y = Life.expectancy.diff, text=paste("Pays : ",Country, "
Espérance de vie en 2000 : ", Life.expectancy, "
Accès à l'eau potable en 2000 : ", Water, "%")), na.rm=TRUE) + geom_point(aes(color=Life.expectancy, size=Water)) + scale_color_gradient(low ="#FFFF99",high = "#009933") + scale_y_continuous(breaks=seq(-10, 20, 5)) + scale_x_continuous(limits=c(-10,35), breaks=seq(-10, 30, 10)) + labs(x = "Différence du pourcentage de personnes \nayant accès à l'eau potable \nentre 2000 et 2018", y = "Différence d'espérance de vie \nentre 2000 et 2018", colour = "Espérance de vie \n en 2000", size = "% de personnes \n ayant accès à l'eau potable en 2000") + ggtitle("Représentation de la rélation entre les différences d'espérance de vie \n et de plusieurs indicateurs entre 2000 et 2018") + theme(plot.title = element_text(face="bold", hjust=0.5, size=10), axis.title.x = element_text(size=9), axis.title.y = element_text(size=9), panel.background = element_rect(fill = "white"), panel.grid.major = element_line(colour = "#CCCCCC"), plot.margin = unit(c(1,1,1,1), "cm")), tooltip="text") # g4 %>% layout(showlegend=TRUE) ``` ```{r} subplot(g1, g2, g3, g4, nrows = 2, margin = 0.1, titleX = T, titleY = T) ``` ### Visualisation 3 ```{r, echo = FALSE, message = FALSE, warning=FALSE} dta_SYR <- subset(dta_off, ISO3 == "SYR") p1 <- ggplot(data = dta_SYR) + geom_line(aes(x = Year, y = Life.expectancy), color = "#00AFBB", size = 1, group=1) + geom_point(aes(x = Year, y = Life.expectancy, group=1)) + scale_y_continuous(limits=c(68,75), breaks=seq(68, 75, 1)) + annotate(geom = "segment", x = 12, xend = 19, y = 68.8, yend = 68.8, arrow = arrow(length = unit(2, "mm"))) + annotate(geom = "text", x = 13, y = 69, label = "Guerre civile depuis 2011", hjust = "left") + theme(plot.margin = unit(c(1,1,1,1), "cm")) + annotate(geom = "segment", x = 0, xend = 19, y = 68, yend = 68, arrow = arrow(length = unit(2, "mm"))) + annotate(geom = "text", x = 9, y = 68.2, label = "Chef du gouvernement : Bashar el-Assad") + annotate(geom = "rect", xmin = 12, xmax = 19, ymin = 68, ymax = 75, alpha = .3, fill = "skyblue3") + annotate(geom = "curve", x = 6.9, xend = 6, y = 74.8, yend = 74.5, curvature = .5, arrow = arrow(length = unit(2, "mm"))) + annotate(geom = "text", x = 7, y = 74.8, label = "Retrait des troupes syriennes du Liban", hjust = "left") + theme_classic() + labs(title="Evolution de l'espérance de vie en Syrie", subtitle='Entre 2000 et 2018', x = 'Années', y = 'Espérance de vie moyenne à la naissance \n (en années)') ``` ```{r, echo = FALSE, message = FALSE, warning=FALSE} p2 <- ggplot(data = dta_off[dta_off$ISO3 == "GRD",], aes(x = Year, y = Life.expectancy, group=1)) + geom_line(color = "#00AFBB", size = 2) + geom_point() + # Ouragan Ivan 2004 annotate(geom = "curve", x = 3.5, xend = 5, y = 73.15, yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2004"] + 0.03, curvature = -0.2, arrow = arrow(length = unit(2, "mm")), col ="#00AFBB") + annotate(geom = "text", x = 3.4, y = 73.15, label = "Ouragan Ivan", hjust = "right", col = "#00AFBB") + geom_segment(aes(x=5, xend=5, y = min(dta_off$Life.expectancy[dta_off$Country=="Grenada"]), yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2004"]), linetype="longdash", col ="#00AFBB") + # Ouragan Emily 2005 annotate(geom = "curve", x = 7.5, xend = 6, y = 73.2, yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2005"] + 0.03, curvature = .2, arrow = arrow(length = unit(2, "mm"))) + annotate(geom = "text", x = 7.6, y = 73.2, label = "Ouragan Emily", hjust = "left") + geom_segment(aes(x=6, xend=6, y = min(dta_off$Life.expectancy[dta_off$Country=="Grenada"]), yend = dta_off$Life.expectancy[dta_off$Country=="Grenada"&dta_off$Year=="2005"]), linetype="longdash") + theme_classic() + labs(title="Evolution de l'espérance de vie de Grenada", # ajout du titre subtitle='Entre 2000 et 2018') # ajout du sous titre ``` ```{r, echo = FALSE, message = FALSE, warning=FALSE} GDP_VEN <- read.csv("GDP_VEN.csv", sep=";") colnames(GDP_VEN) <- c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018") GDP_VEN <- GDP_VEN %>% pivot_longer(c("2000", "2001", "2002","2003", "2004", "2005","2006", "2007", "2008","2009","2010", "2011", "2012","2013", "2014", "2015","2016","2017","2018")) colnames(GDP_VEN)[1:2] <- c("Year", "GDP") GDP_VEN$Year <- as.factor(GDP_VEN$Year) GDP_VEN$GDP_obs <- c(GDP_VEN$GDP[1:15], rep(NA,4)) GDP_VEN$GDP_pred <- c(rep(NA,14),GDP_VEN$GDP[15:19]) dta_dual_y <- data.frame(dta_off[dta_off$ISO3 == "VEN",]) dta_dual_y$GDP_obs <- c(GDP_VEN$GDP[1:15], rep(NA,4)) dta_dual_y$GDP_pred <- c(rep(NA,14),GDP_VEN$GDP[15:19]) old.y <- list( side = "left" ) new.y <- list( overlaying = "y", side = "right", title = "GDP par habitant (USD)" ) p3 <- plot_ly(dta_dual_y) %>% add_lines(x = ~Year, y = ~Life.expectancy, yaxis="y1", name="Esperance de vie", line = list(color = 'black', width = 2)) %>% add_lines(x = ~Year, y = ~GDP_obs, yaxis = "y2", name="GDP (données officielles)", line = list(color = '#00AFBB', width = 2)) %>% add_lines(x = ~Year, y = ~GDP_pred, yaxis = "y2", name="GDP (données estimées)", line = list(color = '#00AFBB', width = 2, dash = 'dot')) %>% layout(yaxis2 = new.y, yaxis = list(old.y, showgrid = F, title="Espérance de vie moyenne à la naissance \n (en années)"), xaxis = list(title="Années", showgrid = F), legend = list(title=list(text=''), y = 73, textfont = list(family = "Arial", size = 14, color = toRGB("black"))), title = list(text = paste0("Evolution de l'espérance de vie du Venezuela", '
', '', "Entre 2000 et 2018"), xanchor = 'left', x=0.1), shapes = list( list(type = "rect", fillcolor = "lightblue", line = list(color = "lightblue"), opacity = 0.2, x0 = "0", x1 = "13", y0 = 72, y1 = 73.5), list(type = "rect", fillcolor = "#009EFF", line = list(color = "#009EFF"), opacity = 0.2, x0 = "13", x1 = "18", y0 = 72, y1 = 73.5))) %>% add_text(showlegend = FALSE, x = c("2004","2016"), y = c(73.4, 73.4), text = c("Présidence d'Hugo Chávez","Présidence de \nNicolás Maduro"), textfont = list(family = "Arial", size = 14, color = toRGB("black"))) ``` ```{r} par(mfrow=c(3,1)) p1 p2 p3 ```